package Help.Manager.Album;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import photos.core.commons.PhotoHolder;


import DTO.PhotoModel;
import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.easynote.R;
import com.tonicartos.widget.stickygridheaders.StickyGridHeadersBaseAdapter;


public class PhotosGroupedAdapter extends BaseAdapter implements StickyGridHeadersBaseAdapter{

	ArrayList<PhotoModel> photos;
	Context context;
	ArrayList<Calendar> headers;
	LayoutInflater inflater;
	float width;
	
	public PhotosGroupedAdapter(Context context, ArrayList<PhotoModel> photos) {
		this.context = context;
		this.photos = photos;
		this.inflater = LayoutInflater.from(this.context);
		
		this.createHeaders();
		
		this.width = context.getResources().getDimension(R.dimen.photo_width);
	}
	
	/*
	 * Tạo danh sách header.
	 */
	private void createHeaders() {
		this.headers = new ArrayList<Calendar>();
		
		for(PhotoModel photo: this.photos) {
			int month = photo.getDate().get(Calendar.MONTH);
			int year = photo.getDate().get(Calendar.YEAR);
			
			if(!isExistHeader(month, year)) {
				Calendar calendar = Calendar.getInstance();
				calendar.set(Calendar.MONTH, month);
				calendar.set(Calendar.YEAR, year);
				this.headers.add(calendar);
			}
		}
	}
	
	/*
	 * Kiểm tra header có tồn tại hay không.
	 */
	private boolean isExistHeader(int month, int year) {
		for(Calendar calendar : this.headers) {
			if(year == calendar.get(Calendar.YEAR)
				&& month == calendar.get(Calendar.MONTH))
				return true;
		}
		
		return false;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View item = convertView;
		PhotoHolder holder;
		
		PhotoModel photo = this.photos.get(position);
		
		if(item == null) {
			item = inflater.inflate(R.layout.photo_item, null);
			
			holder = new PhotoHolder();
			holder.image = (ImageView)item.findViewById(R.id.img);
			
			item.setTag(holder);
		}
		else
		{
			holder = (PhotoHolder)item.getTag();
		}
		
		BitmapHelpers.loadBitmap(
				photo.getPath(), 
				holder.image, 
				this.width, 
				this.width);
		
		return item;
	}
	
	@Override
	public View getHeaderView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View header = convertView;
		HeaderHolder holder;
		if(header == null) {
			header = inflater.inflate(R.layout.header_item, null);
			
			holder = new HeaderHolder();
			holder.tv = (TextView)header.findViewById(R.id.tv);
			
			header.setTag(holder);
		}
		else {
			holder = (HeaderHolder)header.getTag();
		}
		
		Date date = this.headers.get(position).getTime();

		String s = new SimpleDateFormat("MMMM yyyy", Locale.getDefault())
				.format(date);

		holder.tv.setText(s);
		
		return header;
	}
	
	@Override
	public int getCountForHeader(int position) {
		// TODO Auto-generated method stub
		Calendar calendar = this.headers.get(position);
		int i = 0;
		for(PhotoModel photo : this.photos) {
			int result = DateHelpers
					.compareCalendarByMonthYear(calendar, photo.getDate());
			if(result == 0)
				i++;
		}
		return i;
	}

	

	@Override
	public int getNumHeaders() {
		// TODO Auto-generated method stub
		return this.headers.size();
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return this.photos.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return this.photos.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}
	
	@Override
	public boolean areAllItemsEnabled() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isEnabled(int arg0) {
		// TODO Auto-generated method stub
		return true;
	}



	@Override
	public int getItemViewType(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 1;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void registerDataSetObserver(DataSetObserver arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void unregisterDataSetObserver(DataSetObserver arg0) {
		// TODO Auto-generated method stub
		
	}

	class HeaderHolder {
		TextView tv;
	}
}
